home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / zendisk2.zip / LST13-27.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  3KB  |  112 lines

  1. ;
  2. ; *** Listing 13-27 ***
  3. ;
  4. ; Determines whether two zero-terminated strings differ, and
  5. ; if so where, using LODS/SCAS and partial in-line code.
  6. ;
  7.     jmp    Skip
  8. ;
  9. TestString1    label    byte
  10.     db    'This is a test string that is '
  11.     db    'z'
  12.     db    'terminated with a zero byte...',0
  13. TestString2    label    byte
  14.     db    'This is a test string that is '
  15.     db    'a'
  16.     db    'terminated with a zero byte...',0
  17. ;
  18. ; Compares two zero-terminated strings.
  19. ;
  20. ; Input:
  21. ;    DS:SI = first zero-terminated string
  22. ;    ES:DI = second zero-terminated string
  23. ;
  24. ; Output:
  25. ;    DS:SI = pointer to first differing location in
  26. ;        first string, or 0 if the byte wasn't found
  27. ;    ES:DI = pointer to first differing location in
  28. ;        second string, or 0 if the byte wasn't found
  29. ;
  30. ; Registers altered: AX, SI, DI
  31. ;
  32. ; Direction flag cleared
  33. ;
  34. ; Note: Does not handle strings that are longer than 64K
  35. ;    bytes or cross segment boundaries.
  36. ;
  37. CompareStrings:
  38.     cld
  39. CompareStringsLoop:
  40. ;
  41. ; First 7 repetitions of partial in-line code.
  42. ;
  43.     rept    7
  44.     lodsw        ;get the next 2 bytes
  45.     and    al,al    ;is the first byte the terminating
  46.             ; zero?
  47.     jz    CompareStringsFinalByte
  48.             ;yes, so there's only one byte left
  49.             ; to check
  50.     scasw        ;compare this word
  51.     jnz    CompareStringsDifferent ;the strings differ
  52.     and    ah,ah    ;is the second byte the terminating
  53.             ; zero?
  54.     jz    CompareStringsSame
  55.             ;yes, we've got a match
  56.     endm
  57. ;
  58. ; Final repetition of partial in-line code.
  59. ;
  60.     lodsw        ;get the next 2 bytes
  61.     and    al,al    ;is the first byte the terminating
  62.             ; zero?
  63.     jz    CompareStringsFinalByte
  64.             ;yes, so there's only one byte left
  65.             ; to check
  66.     scasw        ;compare this word
  67.     jnz    CompareStringsDifferent ;the strings differ
  68.     and    ah,ah    ;is the second byte the terminating
  69.             ; zero?
  70.     jnz    CompareStringsLoop ;no, continue comparing
  71.             ;the strings are the same
  72. CompareStringsSame:
  73.     sub    si,si    ;return 0 pointers indicating that
  74.     mov    di,si    ; the strings are identical
  75.     ret
  76. CompareStringsFinalByte:
  77.     scasb        ;does the terminating zero match in
  78.             ; the 2 strings?
  79.     jz    CompareStringsSame ;yes, the strings match
  80.     dec    si    ;point back to the differing byte
  81.     dec    di    ; in each string
  82.     ret
  83. CompareStringsDifferent:
  84.             ;the strings are different, so we
  85.             ; have to figure which byte in the
  86.             ; word just compared was the first
  87.             ; difference
  88.     dec    si
  89.     dec    si    ;point back to the first byte of the
  90.     dec    di    ; differing word in each string
  91.     dec    di
  92.     lodsb
  93.     scasb        ;compare that first byte again
  94.     jz    CompareStringsDone
  95.             ;if the first bytes are the same,
  96.             ; then it must have been the second
  97.             ; bytes that differed. That's where
  98.             ; we're pointing, so we're done
  99.     dec    si    ;the first bytes differed, so point
  100.     dec    di    ; back to them
  101. CompareStringsDone:
  102.     ret
  103. ;
  104. Skip:
  105.     call    ZTimerOn
  106.     mov    si,offset TestString1 ;point to one string
  107.     mov    di,seg TestString2
  108.     mov    es,di
  109.     mov    di,offset TestString2 ;point to other string
  110.     call    CompareStrings    ;and compare the strings
  111.     call    ZTimerOff
  112.